package org.flow.basic.io.file_io;

import java.io.*;

/**
 * 传统文件传输
 *
 * @author xl-9527
 * @since 2025/2/21
 **/
public class CommonStreamFileTransfers {

    private static final int FILE_SIZE = 1024;

    /**
     * 传输 begin -> end
     *
     * @param begin 原始文件
     * @param end 拷贝以后的新文件
     */
    public void transfer(final File begin, final File end) {
        try (
                BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(begin));
                BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(end))
        ) {
            byte[] bytes = new byte[FILE_SIZE];
            int currentReadBytes;

            while ((currentReadBytes = inputStream.read(bytes)) != -1) {
                System.out.println("读取：" + new String(bytes, 0, currentReadBytes));
                // out put stream
                outputStream.write(bytes, 0, currentReadBytes);
            }

            System.out.println("读取完毕");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
